<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="FCK" uri="http://java.fckeditor.net" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>网络服务平台</title>
<link rel="stylesheet" type="text/css" href="../../../../js/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="../../../../js/easyui/themes/icon.css">
<script type="text/javascript" src="../../../../js/easyui/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../../../../js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../../../../js/easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
$(function() {
    $('#topNewsGrid').datagrid({
        title:'推荐新闻列表',
        iconCls:'icon-save',
        width:"100%",
        height:430,
        nowrap: false,
        striped: true,
        collapsible:true,
        url:'/management/news/fetchSuggestNews.action',
        queryParams:{},
        remoteSort: false,
        idField:'id',
        frozenColumns:[
            [
                {field:'ck',checkbox:true},
                {"width":400,"field":"title","title":"标题",
                    formatter:function(value, rowData, rowIndex) {
                        return '<a target="_blank" href="/management/news/showNews.action?id=' + rowData.id + ' ">' + value + '</a>';
                    }
                }
            ]
        ],
        columns:[
            [
                {"width":600,"field":"keywords","title":"关键字"},
                {"width":180,"field":"createDate","title":"发布日期"}
            ]
        ],
        pagination:true,
        rownumbers:true,
        toolbar:[
            {"iconCls":"icon-add","handler":function() {
                showAddSuggestNews();
            },"text":"添加推荐","title":"添加推荐","id":"btnAdd"}
            <s:if test="1 == #session._user_role_">
            ,
            {"iconCls":"icon-remove","handler":function() {
                deleteSuggestNews();
            },"text":"删除推荐新闻","title":"删除推荐新闻","id":"btnOpe"}
            </s:if>
        ]
    });

    var p = $('#topNewsGrid').datagrid('getPager');
    if (p) {
        $(p).pagination({
            onBeforeRefresh:function() {
            }
        });
    }
});

function showAddSuggestNews() {
    $('#addTopNewsDialog').window('open');
    initNewsDataGrid();
}

function deleteSuggestNews() {
    var rows = $('#topNewsGrid').datagrid('getSelections');
    if (rows.length < 1) {
        return;
    }

    $.messager.confirm('推荐新闻', '确定要删除选中的推荐新闻？', function(r) {
        if (r) {
            var ids = "";
            for (var i = 0; i < rows.length; i++) {
                ids += rows[i].id + ",";
            }
            ids = ids.substring(0, ids.length - 1);

            $("#idString").val(ids);

            $("#editTopNewsForm").form('submit', {
                url: "/management/news/deleteSuggestNews.action",
                success:function(data) {
                    if (data.match('true')) {
                        $.messager.show({
                            title:'提示信息',
                            msg:'删除推荐新闻成功！'
                        });

                        deleteSelectedRows("topNewsGrid");
                    } else {
                        $.messager.show({
                            title:'提示信息',
                            msg:'删除推荐新闻失败！'
                        });
                    }
                }
            });
        }
    });
}

function deleteSelectedRows(gridId) {
    var rows = $('#' + gridId).datagrid('getSelections');
    var length = rows.length;

    if (length < 1) {
        return;
    }

    for (var i = 0; i < length; i++) {
        var selectedRow = $('#' + gridId).datagrid('getSelected');
        var index = $('#' + gridId).datagrid('getRowIndex', selectedRow);
        $('#' + gridId).datagrid('deleteRow', index);
    }
}

function initNewsDataGrid() {
    $('#newsGrid').datagrid({
        title:'新闻列表',
        iconCls:'icon-save',
        width:"100%",
        height:380,
        nowrap: false,
        striped: true,
        collapsible:true,
        url:'/management/news/fetchNonSuggestNews.action',
        remoteSort: false,
        idField:'id',
        frozenColumns:[
            [
                {field:'ck',checkbox:true},
                {"width":350,"field":"title","title":"标题",
                    formatter:function(value, rowData, rowIndex) {
                        return '<a target="_blank" href="/management/news/showNews.action?id=' + rowData.id + ' ">' + value + '</a>';
                    }
                }
            ]
        ],
        columns:[
            [
                {"width":240,"field":"keywords","title":"关键字"},
                {"width":180,"field":"createDate","title":"发布日期"}
            ]
        ],
        pagination:true,
        rownumbers:true
       /* toolbar:[
            {"iconCls":"icon-add","handler":function() {
                addSuggestNews();
            },"text":"添加推荐新闻","title":"添加推荐新闻","id":"btnAdd"}

        ]*/
    });

    var pager = $('#newsGrid').datagrid('getPager');
    if (pager) {
        $(pager).pagination({
            onBeforeRefresh:function() {

            }
        });
    }
}

function addSuggestNews() {
    var rows = $('#newsGrid').datagrid('getSelections');

    if (rows.length < 1) {
        return;
    }

    var ids = "";
    var length = rows.length;
    for (var i = 0; i < length; i++) {
        ids += rows[i].id + ",";
    }

    ids = ids.substring(0, ids.length - 1);

    $("#idString").val(ids);

    $.messager.progress({
        title:'提示信息',
        msg:'添加推荐新闻......！'
    });

    $("#editTopNewsForm").form('submit', {
        url: "/management/news/addSuggestNews.action",
        success:function(data) {
            if (data.match('true')) {
                $.messager.show({
                    title:'提示信息',
                    msg:'添加推荐新闻成功！'
                });

                deleteSelectedRows("newsGrid");
            } else {
                $.messager.show({
                    title:'提示信息',
                    msg:'添加推荐新闻失败！'
                });
            }

            $.messager.progress("close");
            closeAddSuggestNewsDialog();
        }
    });
}

function queryNews() {
    var newsTitle = $("#newsTitle").val();
    var newsColumn = $("input[name='newsColumn']").val();
    var queryParams = $('#newsGrid').datagrid('options').queryParams;
    queryParams.newsTitle = newsTitle;
    queryParams.newsColumn = newsColumn;
    $('#newsGrid').datagrid('options').queryParams = queryParams;
    $("#newsGrid").datagrid('reload');
}

function getSelected() {
    var selected = $("#topNewsGrid").datagrid("getSelected");
    return selected;
}

function getSelections() {
    var rows = $("#topNewsGrid").datagrid("getSelections");
    return rows;
}

function closeAddSuggestNewsDialog() {
    $('#addTopNewsDialog').window('close');
    $('#topNewsGrid').datagrid('reload');
}

</script>

</head>

<body class="easyui-layout">

<div region="center" style="height:600px;padding:0px;background:#efefef;">
    <div id="p1" class="easyui-panel" style="height:450px;">`
        <table id="topNewsGrid"></table>
    </div>
</div>


<div id="addTopNewsDialog" closed="true" modal="true" class="easyui-window" title="添加推荐新闻" iconCls="icon-save"
     style="width:880px; height:560px;padding:5px;background: #fafafa;">
    <div class="easyui-layout" fit="true">
        <div region="center" border="false" style="padding:10px;background:#fff;border:1px solid #ccc;">
            <div id="p" class="easyui-panel" title="新闻搜索" icon="icon-search"
                 style="width: 800px; height:80px;padding:10px;">
                <table>
                    <tr>
                        <td><label>标题:</label><input type="hidden" id="currentNewsId"/></td>
                        <td><input name="newsTitle" id="newsTitle"/></td>
                        <td><label>栏目:</label></td>
                        <td><select id="columnCombo" class="easyui-combotree" name="newsColumn"
                                    url="/management/columns/fetchColumnsForTree.action"
                                    cascadeCheck="false" style="width:200px;"></select></td>

                        <td style="width:30px"></td>
                        <td><a class="easyui-linkbutton" onclick="queryNews()">搜索</a></td>
                    </tr>
                </table>
            </div>
            <div>
                <table id="newsGrid"></table>
            </div>
            <form id="editTopNewsForm" method="post">
                <input type="hidden" name="idString" id="idString"/>
            </form>
        </div>
        <div region="south" border="false" style="text-align:right;height:30px;line-height:30px;">
            <a class="easyui-linkbutton" iconCls="icon-ok" href="javascript:void(0)"
               onclick="addSuggestNews()">确定</a>
        </div>
    </div>
</div>
</body>
</html>